﻿package com.prasanna.practice.utils
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	

	/**
	 * ...
	 * @author Vikram Kute
	 *  
	 */
	
	public class SwfLoader extends EventDispatcher
	{
		private var _swfURL:String;

		public function SwfLoader() 
		{
			
		}
		public function startLoad() 
		{
			var mLoader:Loader = new Loader();
			var mRequest:URLRequest = new URLRequest(swfURL);
			mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			mLoader.load(mRequest);
		}

		private function onCompleteHandler(loadEvent:Event) 
		{			
			var loadedSwf:MovieClip = loadEvent.currentTarget.content as MovieClip;			
			var evt:SwfLoaderEvent = new SwfLoaderEvent(SwfLoaderEvent.SWF_LOADED);
			
			evt.loadedSwf = loadedSwf;
			dispatchEvent(evt);
		}
		
		private function onProgressHandler(mProgress:ProgressEvent) 
		{
			var percent:Number = Math.round(mProgress.bytesLoaded / mProgress.bytesTotal * 100);		
			var evt:SwfLoaderEvent = new SwfLoaderEvent(SwfLoaderEvent.SWF_PROGRESS);
			evt.progress = percent;
			dispatchEvent(evt);
			//ToolsModel.progressMc.progress_txt.text = String(percent) + " %";
			//ToolsModel.progressMc.pbar_mc.width = percent * 2;
		}
		
		public function get swfURL():String 
		{ 
			return _swfURL; 
		}
		
		public function set swfURL(value:String):void 
		{
			_swfURL = value;
			startLoad();
		}
	}
}